using System;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Common.Utilities;

namespace Workflows.Components.Reports
{
    public class StringFieldAggregate : AggregateBase
    {
        private string _Min;
        private string _Max;
        private string _Median;

        private Dictionary<string, int> _Spread;

        public StringFieldAggregate(string connStr, string tblName, string colName)
            : base(connStr, tblName, colName)
        {
            this.SummarizeData(connStr, tblName, colName);
        }

        private void SummarizeData(string connStr, string tblName, string colName)
        {
            if (this.Type != AggregateDataType.String)
            {
                throw new Exception("This class only supports number string type");
            }
            

            SqlConnection conn = new SqlConnection(connStr);
            try
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = "select top 1 [" + colName + "] from [" +
                    tblName + "] where [" + colName + "] is not null " +
                    "order by [" + colName + "]";
                SqlDataReader reader = cmd.ExecuteReader();
                int index = 0;
                string currentValue = string.Empty;
                if (reader.Read())
                {
                    this._Min = reader[0].ToString();
                    currentValue = this._Min;
                    index++;
                }
                while (reader.Read() && index < this.Count / 2)
                {
                    currentValue = reader[0].ToString();
                    index++;
                }
                reader.Close();
                this._Median = currentValue;

                cmd.CommandText = "select top 1 [" + colName + "] from [" +
                    tblName + "] where [" + colName + "] is not null " +
                    "order by [" + colName + "] desc";
                reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    this._Max = reader[0].ToString();
                }
                reader.Close();

                cmd.CommandText = "select [" + colName + "], count ([" + colName +
                    "]) from [" + tblName + "] where [" + colName +
                    "] is not null group by [" + colName +
                    "] order by count ([" + colName + "]) desc";
                reader = cmd.ExecuteReader();
                this._Spread = new Dictionary<string, int>();
                while (reader.Read())
                {
                    string stringKey = reader[0].ToString();
                    int count = reader.GetInt32(1);
                    this.Spread.Add(stringKey, count);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandlesException(ex);
            }
            finally
            {
                conn.Close();
            }
        }

        #region properties
        [Browsable(true), Category("Stats")]
        public Dictionary<string, int> Spread
        {
            get
            {
                return this._Spread;
            }
        }
        [Browsable(true), Category("Stats")]
        public string Max
        {
            get { return _Max; }
        }
        [Browsable(true), Category("Stats")]
        public string Min
        {
            get { return _Min; }
        }
        [Browsable(true), Category("Stats")]
        public string Median
        {
            get { return _Median; }
        }
        [Browsable(true), Category("Stats")]
        public string Mean
        {
            get
            {
                return null;
            }
        }
        #endregion
    }
}
